﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Configuration;
using System.Linq;
using FriendsNearby.Services;
using FriendsNearby.Services.Entities;
using FriendsNearby.Services.Tests.Mocks;
using InSTEDD.Labs.Geo;
using InSTEDD.Labs.Geo.GeoCoding.Google;
using NUnit.Framework;
using Twitter;
using Twitter.Tests;

namespace FriendsNearbyBot.Tests
{
	[TestFixture]
	public class IntegrationFixture
	{
		[Test]
		public void ShouldRunThroughProcessors()
		{
			IServiceProvider services = CreateServices();
			string botUser = "fnbtesta";
			string clientUser = "fnbtestb";
			MemoryBotClient bot = new MemoryBotClient("fnbtesta");
			MemoryBotClient client = new MemoryBotClient("fnbtestb");
			bot.SetBuddy(client);
			client.SetBuddy(bot);

			UpdateLocationProcessor updater = new UpdateLocationProcessor(services);
			HelpProcessor help = new HelpProcessor();
            DefaultProcessor defaultProcessor = new DefaultProcessor();

			BotProcessor processor = new BotProcessor(
				bot,
				new Greeter(),
                defaultProcessor,
				updater,
				help);

			client.Friendship.Follow(botUser);

            processor.Run();

            Assert.That(bot.Friendship.GetFriends().Contains(clientUser));
			Assert.AreEqual(Properties.Resources.GreetMessage, client.Messages.GetLatest().Text);

			client.Messages.Send("fnbtesta", "?");

            processor.Run();

            //Assert.AreEqual(Properties.Resources.HelpMessage, client.Messages.GetLatest().Text);

			client.Messages.Send("fnbtesta", "at -75*65");

			services.GetService<IUsersService, MockUsersService>().SetUserPreferences(
				new UserPreference { FacebookUserId = "fnbtestb", TwitterScreenName = "fnbtestb" });
            
            processor.Run();
            IEnumerable<Message> msgs = client.Messages.GetAll();
			AssertString.StartsWith("We got your location update", client.Messages.GetLatest().Text);
		}


		private IServiceProvider CreateServices()
		{
			ServiceContainer container = new ServiceContainer();
            MockUsersService userService = new MockUsersService();
            userService.Preferences.Add("831028231", new UserPreference { FacebookUserId = "831028231", TwitterScreenName = "fnbtesta" });
            userService.Preferences.Add("830472205", new UserPreference { FacebookUserId = "830472205", TwitterScreenName = "fnbtestb" });
            container.AddService<ILocationService>(new LocationService(new MockPeopleNearby()));
            container.AddService<IUsersService>(userService);
			container.AddService<ISocialNetworkService>(new MockSocialNetworkService());
			container.AddService<IPeopleNearbyService>(new MockPeopleNearby());

			return container;
		}
	}
}
